/**
  ******************************************************************************
  * @file    Project/STM32F10x_StdPeriph_Template/main.c 
  * @author  uLuxx
  * @version V1.0.0
  * @date    14-August-2014
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2014</center></h2>
  ******************************************************************************
  */  

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>
#include "platform_config.h"
#include "main.h"
#include "system.h"
#include <intrinsics.h>


/** @addtogroup MSMbGSF
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
    
#define mainFreq        100     /* Every x milliseconds the main loop is served */

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile unsigned int milliseconds;
unsigned int lastStamp;

unsigned char SPITransmitbuffer[4];

EGTData EGT;
AFRData AFR;
RPMData RPM;
ECTData ECT;
baroData barometric;
TPSData TPS;
NTRLData neutral;
speedoData speedo;


/* Private function prototypes -----------------------------------------------*/
void initOnChipHW(void);
void SPI_Configuration(void);
void UART_Configuration(void);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void DMA_Configuration(void);
void Timer_Configuration(void);
void ADC_Configuration(void);
void CAN_Configuration(void);

void triggerDataCapture(void);


/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */    
         
    /* Setup SysTick Timer for 1 msec interrupts.
     ------------------------------------------
    1. The SysTick_Config() function is a CMSIS function which configure:
       - The SysTick Reload register with value passed as function parameter.
       - Configure the SysTick IRQ priority to the lowest value (0x0F).
       - Reset the SysTick Counter register.
       - Configure the SysTick Counter clock source to be Core Clock Source (HCLK).
       - Enable the SysTick Interrupt.
       - Start the SysTick Counter.
    
    2. You can change the SysTick Clock source to be HCLK_Div8 by calling the
       SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8) just after the
       SysTick_Config() function call. The SysTick_CLKSourceConfig() is defined
       inside the misc.c file.

    3. You can change the SysTick IRQ priority by calling the
       NVIC_SetPriority(SysTick_IRQn,...) just after the SysTick_Config() function 
       call. The NVIC_SetPriority() is defined inside the core_cm3.h file.

    4. To adjust the SysTick time base, use the following formula:
                            
         Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)
    
       - Reload Value is the parameter to be passed for SysTick_Config() function
       - Reload Value should not exceed 0xFFFFFF
   */
  
  SystemCoreClockUpdate();
  
  milliseconds = 0;
  lastStamp = 0;
  
  if (SysTick_Config(SystemCoreClock / 1000))
  { 
    /* Capture error */ 
    while (1)
    {
      /* Show we can't start because of SysTick-Problem */
      ;
    }
  }

  
  /* Init HW-Modules */
  initOnChipHW();
  
  /* Prepare data */
  
  /* SPI-Buffer used to "transmit" data from SPI-driven EGT */
  SPITransmitbuffer[0] = 0xFF;
  SPITransmitbuffer[1] = 0xFF;
  SPITransmitbuffer[2] = 0xFF;
  SPITransmitbuffer[3] = 0xFF;
  
  EGT.status = EGTNODATA;
  EGT.temperature = 0;
#ifdef EGTONBOARD
  EGT.source = SRCINT;
#else  /* EGTONBOARD */
  EGT.source = SRCEXT;
#endif /* EGTONBOARD */
  
  AFR.status = AFRNODATA;
  AFR.afr = 0;
#ifdef AFRONBOARD
  AFR.source = SRCINT;
#else  /* AFRONBOARD */
  AFR.source = SRCEXT;
#endif /* AFRONBOARD */
  
  
  RPM.status = RPMNODATA;
  RPM.rpm = 0;
  
  ECT.status = ECTNODATA;
  ECT.temperature = 0;
  
  
  
  

  /* System is online, start main loop */
  triggerDataCapture();
  
  /* Infinite loop */
  while (1)
  {
    /* Main loop is served only in intervals */
    unsigned int current;
    getSystemTime(&current);
    if(current - lastStamp > mainFreq)
    {
      lastStamp = current;
      
      /* Execute tasks */
      
      /* All data is gathered interrupt and DMA based, so we need to copy, extract and convert it now */
      updateData();
      
      /* Call system supervisor */
      SystemSVS();
      
      /* Call engine supervisor */
      EngineSVS();
      
      /* Call flashercontrol */
      flasherControl();
      
      /* Call CAN-Handler */
      CANHandler();  
      
      /* Trigger new data capture */
      triggerDataCapture();
      
    }    
  }
}

/**
  * @brief  Initializes the onChip-HW needed, e.g. CAN, UART, SPI, Timers
  * @param  None
  * @retval None
  */
void initOnChipHW(void)
{
  /* Enable clock for all modules */
  // TODO: Fix this, bring all modules up! uLuxx 20140815
  RCC_Configuration();
  
  /* Start with DMA, which is needed for UART, SPI and CAN */
  DMA_Configuration();
  
  /* Next UART for diagnosis etc*/
  UART_Configuration();
  
  /* SPI for EGT */
  SPI_Configuration();
  
  /* Timer for RPM-Gathering */
  Timer_Configuration();
  
  /* IO for sensing neutral */
  GPIO_Configuration();
  
  /* ADC for monitoring AFR and battery voltage */
  ADC_Configuration();
  
  /* CAN for communication with other modules on vehicle */
  CAN_Configuration();
  
}

/**
  * @brief  Configures the ADC for the monitoring of AFR and battery voltage
  * @param  None
  * @retval None
  */
void ADC_Configuration(void)
{
  /* Make sure the clock for ADC1 is running before we come here */
  
  
  /* Prepare IOs */
  GPIO_InitTypeDef GPIO_InitStructure;

  /* AFR Input */
  GPIO_InitStructure.GPIO_Pin =  ADC_AFR_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 
  GPIO_Init(ADC_AFR_GPIO, &GPIO_InitStructure);
  
  /* Battery input */
  GPIO_InitStructure.GPIO_Pin =  ADC_Battery_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 
  GPIO_Init(ADC_Battery_GPIO, &GPIO_InitStructure);
  
  ADC_InitTypeDef ADC_InitStructure;
  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 2;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_AFR, 1, ADC_SampleTime_55Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_Battery, 1, ADC_SampleTime_55Cycles5);

  // TODO: Check if we use DMA!!
  // uLuxx 20140815
  /* Enable ADC1 DMA */
  //ADC_DMACmd(ADC1, ENABLE);

  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
   
  // This is the way to trigger the conversion lateron!
  /* Start ADC1 Software Conversion */ 
  //ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}

/**
  * @brief  Configures the CAN module 
  * @param  None
  * @retval None
  */
void CAN_Configuration(void)
{
  
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  
  GPIO_InitTypeDef  GPIO_InitStructure;  
  
  /* Configure CAN pin: RX */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_RX;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
  
  /* Configure CAN pin: TX */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_TX;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIO_CAN, &GPIO_InitStructure);
  
  GPIO_PinRemapConfig(GPIO_Remapping_CAN , ENABLE);
  
  /* CAN register init */
  CAN_DeInit(CAN1);
  CAN_StructInit(&CAN_InitStructure);

  /* CAN cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE;
  CAN_InitStructure.CAN_ABOM = DISABLE;
  CAN_InitStructure.CAN_AWUM = DISABLE;
  CAN_InitStructure.CAN_NART = DISABLE;
  CAN_InitStructure.CAN_RFLM = DISABLE;
  CAN_InitStructure.CAN_TXFP = DISABLE;
  CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
  
  /* CAN Baudrate = 1MBps*/
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
  CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
  CAN_InitStructure.CAN_Prescaler = 4;
  CAN_Init(CAN1, &CAN_InitStructure);

  /* CAN filter init */
  CAN_FilterInitStructure.CAN_FilterNumber = 0;

  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
  CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);
  
  /* Transmit */
//  TxMessage.StdId = 0x321;
//  TxMessage.ExtId = 0x01;
//  TxMessage.RTR = CAN_RTR_DATA;
//  TxMessage.IDE = CAN_ID_STD;
//  TxMessage.DLC = 1;
  
}

/**
  * @brief  Configures the Timer used for the VR-Input
  * @param  None
  * @retval None
  */
void Timer_Configuration(void)
{ 
  /* Expecting the clock for TIM2 already running */
  
  TIM_DeInit(TIM2);
  
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  
  /* Calculate prescaler */
  uint16_t PrescalerValue = (uint16_t) (SystemCoreClock / 1125000);
    
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  
  /* Init Capture-IO */
  GPIO_InitTypeDef GPIO_InitStructure;

  /* TIM2 channel 2 pin configuration */
  GPIO_InitStructure.GPIO_Pin =  VRIN_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(VRIN_GPIO, &GPIO_InitStructure);
  
  TIM_ICInitTypeDef TIM_ICInitStructure;
  
  
  /* TIM2 configuration: Input Capture mode ---------------------
     The external signal is connected to TIM2 CH2 pin   
     The Rising edge is used as active edge,
     The TIM2 CCR2 is used to compute the frequency value 
  ------------------------------------------------------------ */

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_ICInit(TIM2, &TIM_ICInitStructure);
  
  /* TIM enable counter */
  TIM_Cmd(TIM2, ENABLE);

  // TODO: Evaluate which interrupt behavior is best 
  // uLuxx 20140815
  
  /* Enable the CC2 Interrupt Request */
 // TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
  
}

/**
  * @brief  Configures the SPI used for the EGT measurement (MAXIM IC)
  * @param  None
  * @retval None
  */
void SPI_Configuration(void)
{ 
  /* Clock for the needed SPI-Module must be enabled before we arrive here */
  GPIO_InitTypeDef GPIO_InitStructure;
  
  GPIO_PinRemapConfig(SPI_MASTER_REMAP, ENABLE);
  
  /* Init IOs needed for SPI */
  /* Configure SPI_MASTER pins: SCK */
  GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SCK;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);

  /* Configure SPI_MASTER CS pin */
  GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_CS;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);
  
  /* Pre Init CS Pin */
  GPIO_WriteBit(SPI_MASTER_GPIO, SPI_MASTER_PIN_CS, Bit_SET);
  
  /* Configure SPI_MASTER MISO pin */
  GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_MISO;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);
  
  SPI_Cmd(SPI_MASTER, DISABLE);
  
  SPI_InitTypeDef  SPI_InitStructure;
  SPI_StructInit(&SPI_InitStructure);
  
  
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; /* Resulting in a max. frequ. below 5MHz */
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI_MASTER, &SPI_InitStructure);
  
  /* Enable SPI_MASTER */
  SPI_Cmd(SPI_MASTER, ENABLE);
  
  
  
  
  // TODO: Not yet enabled, let's see how we do this uLuxx 20140815
  /* Enable DMA1 Channel4 */
 // DMA_Cmd(SPI_SLAVE_Rx_DMA_Channel, ENABLE);
  
}

/**
  * @brief  Configures the UART used for diagnosis and data-output.
  * @param  None
  * @retval None
  */
void UART_Configuration(void)
{   
  /* Expect the Clock already running for this module once we come here */
  /* GPIOs needed for this module are configured in here */
  
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable the USART1 Pins Software Remapping */
  GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);


  /* Configure USART1 Rx as input floating */
  GPIO_InitStructure.GPIO_Pin = USART1_RxPin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
   
  
  /* Configure USART1 Tx as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = USART1_TxPin;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
  
  
  USART_InitTypeDef USART_InitStructure;
  USART_DeInit(USART1);
  
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  
  /* Configure USART1 */
  USART_Init(USART1, &USART_InitStructure);  

  // The following is not needed yet!
  // Later we will use DMA for sending and IT for receiving.
  // uLuxx 20140815
  
//  /* Enable USART1 DMA TX request */
//  USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
//
//  /* Enable the USART1 Receive Interrupt */
//  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  
  /* Enable USART1 */
  USART_Cmd(USART1, ENABLE);  

//  /* Enable USART1 DMA TX Channel */
//  DMA_Cmd(USART1_Tx_DMA_Channel, ENABLE); 
  
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
  /* DMA clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

  /* Enable GPIO AFIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  
  /* Enable ADC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
  /* Enable general GPIO clock */
  RCC_APB2PeriphClockCmd(GENERAL_GPIO_CLK, ENABLE);
  
  /* Enable USART1 GPIO clock */
  RCC_APB2PeriphClockCmd(USART1_GPIO_CLK, ENABLE);

  /* Enable USART1 clock */
  RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE);
  
  /* TIM2 GPIO clock enable */
  RCC_APB2PeriphClockCmd(TIM2_CAPTURE_GPIO_CLK, ENABLE);
  
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* SPI1 GPIO clock enable */
  RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK, ENABLE);
  
  /* SPI1 clock enable */
  RCC_APB2PeriphClockCmd(SPI_MASTER_CLK, ENABLE);
    
  /* CAN1 GPIO clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN1, ENABLE);
  
  /* CAN1 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
   
}

/**
  * @brief  Configures the different GPIO ports.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

//#ifdef USE_STM3210C_EVAL
//  /* Enable the USART3 Pins Software Remapping */
//  GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
//  
//  /* Enable the USART2 Pins Software Remapping */
//  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);  
//#elif defined(USE_STM3210B_EVAL) || defined(USE_STM32100B_EVAL)
//  /* Enable the USART2 Pins Software Remapping */
//  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
//#endif
//
//  /* Configure USARTy Rx as input floating */
//  GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//  GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
//  
//  /* Configure USARTz Rx as input floating */
//  GPIO_InitStructure.GPIO_Pin = USARTz_RxPin;
//  GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);  
//  
//  /* Configure USARTy Tx as alternate function push-pull */
//  GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
//  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//  GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
//
//  /* Configure USARTz Tx as alternate function push-pull */
//  GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
//  GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);  
}

/**
  * @brief  Configures the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
   NVIC_InitTypeDef NVIC_InitStructure;

//  /* Enable the USARTz Interrupt */
//  NVIC_InitStructure.NVIC_IRQChannel = USARTz_IRQn;
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  Configures the DMA.
  * @param  None
  * @retval None
  */
void DMA_Configuration(void)
{
  DMA_InitTypeDef DMA_InitStructure;

//  /* USARTy_Tx_DMA_Channel (triggered by USARTy Tx event) Config */
//  DMA_DeInit(USARTy_Tx_DMA_Channel);
//  DMA_InitStructure.DMA_PeripheralBaseAddr = USARTy_DR_Base;
//  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer1;
//  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//  DMA_InitStructure.DMA_BufferSize = TxBufferSize1;
//  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
//  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
//  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
//  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
//  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//  DMA_Init(USARTy_Tx_DMA_Channel, &DMA_InitStructure);
//  
//  /* USARTz_Tx_DMA_Channel (triggered by USARTz Tx event) Config */
//  DMA_DeInit(USARTz_Tx_DMA_Channel);
//  DMA_InitStructure.DMA_PeripheralBaseAddr = USARTz_DR_Base;
//  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer2;
//  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
//  DMA_InitStructure.DMA_BufferSize = TxBufferSize2;
//  DMA_Init(USARTz_Tx_DMA_Channel, &DMA_InitStructure);
}

/**
  * @brief  Is called by the SysTick-Interrupt
  * @param  None
  * @retval None
  */
void systemTimer(void)
{
  milliseconds++;
}

/**
  * @brief  Stores the current milliseconds since system-start (wrapped at max. value) at the pointer
  * @param  unsigned int *time
  * @retval None
  */
void getSystemTime(unsigned int *time)
{
  __disable_interrupt();
  *time = milliseconds;
  __enable_interrupt();  
}

/**
  * @brief  Triggers the capture of new data
  * @param  None
  * @retval None
  */
void triggerDataCapture(void)
{
  /* Trigger ADC */
  
  /* Trigger SPI */
  
  
  
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */


/******************* (C) COPYRIGHT 2014 *****END OF FILE****/
